const Service = require('egg').Service;

class booksService extends Service {
  //获取所有书籍
  async selectAllBooks() {
    const sql = 'select * from library';
    let res = await this.ctx.app.mysql.query(sql);
    return res;
  }
  //分页查询书籍		
  // let nowPage = 当前页
  // let totalPage = 总页数
  // let maxResult = 每页最大条数
  async selectBooksCutPage(nowPage, maxResult) {
    let index = (nowPage - 1) * maxResult;
    const sql = 'select * from library limit ?, ?';
    let res = await this.ctx.app.mysql.query(sql, [index, maxResult]);
    return res;
  }
  //增加书籍
  async insertBooks(booksinfo) {
    // console.log("1111",booksinfo);
    const sql = 'insert into library (name,numbering,sort, synopsis)values(?,?,?,?)';
    let res = await this.ctx.app.mysql.query(sql, 
      [booksinfo.name, booksinfo.numbering, booksinfo.sort, booksinfo.synopsis]);
    return res;
}

//查询书籍
async searchBookss(searchinfo) {
  const { ctx } = this;
  // console.log("1111",searchinfo);
  const sql1 = `select * from library  where name like '%${searchinfo.value}%' `;
  const sql2 = `select * from library  where numbering like '%${searchinfo.value}%' `;
  let res = await ctx.app.mysql.query(sql1,sql2);
  console.log(res);
  return res;
}

//修改书籍
async editBooks(editbooksinfo,id) {
  // console.log("1111",booksinfo);
  const sql = 'update library set name=?,numbering=?,sort=?,synopsis=? where id=?';
  let res = await this.ctx.app.mysql.query(sql, 
    [editbooksinfo.name, editbooksinfo.numbering, editbooksinfo.sort, editbooksinfo.synopsis,id]);
  return res;
}

//删除书籍
async removeBooks(deleteid) {
  // console.log("1111",deleteid);
  const sql = 'delete from library where id=? ';
  let res = await this.ctx.app.mysql.query(sql,[deleteid.deleteid]);
  return res;
}

}
module.exports = booksService;